<template>
	<view>
		<!-- 地图显示 -->
		<orderMap :address="orderForm.address"></orderMap>
		<!-- 订单详情 -->
		<view class="order-views">
			<view class="grid-box">
				<view class="u-flex">
					<view class="u-m-r-20"><u-icon name="clock" size="80" :color="$u.color.infoDark"></u-icon></view>
					<view class="u-flex-1">
						<view class="head-title u-m-b-10"><text>{{orderForm.managestate}}</text></view>
						<view class="tips-txt">请与客户分拣清算物品</view>
					</view>
				</view>
			</view>
			<view class="grid-box">
				<view class="u-flex">
					<view class="u-flex-1" @click="goMap(orderForm.address)">
						<text class="u-font-36 u-font-bold">[{{orderForm.address.cityname}}]</text>
						<text class="u-font-36">{{orderForm.address.address}}</text>
					</view>
					<view>
						<text class="iconfont icon-youjiantou u-tips-color"></text>
					</view>
				</view>
				<view class="u-m-t-20">
					<text class="u-tips-color">{{orderForm.address.name}}</text>
					<text class="u-tips-color u-p-l-10">{{orderForm.address.mobile}}</text>
				</view>
				<view class="u-flex u-p-t-20">
					<view class="u-m-r-10"><u-button size="mini" @click="call(orderForm.address.mobile)"><u-icon name="phone-fill" size="24"></u-icon>电话联系</u-button></view>
				</view>
			</view>
			<view class="grid-box">
				<view class="head-title u-m-b-20"><text>订单信息</text></view>
				<view class="msg-box">
					<view class="item">
						<text class="label">订单编号：</text>
						<view class="u-flex u-flex-1">
							<view class="u-flex-1">
								<view class="text">{{orderForm.number}}</view>
							</view>
							<view>
								<text class="u-tips-color u-p-l-20 copy" @click="copy(orderForm.number)">复制</text>
							</view>
						</view>
					</view>
					<view class="item">
						<text class="label">下单时间：</text>
						<view class="text">{{$u.timeFormat(orderForm.createddatetime, 'yyyy-mm-dd hh:MM:ss')}}</view>
					</view>
					<view class="item">
						<text class="label">上门时间：</text>
						<view class="text" v-if="orderForm.doortime">{{$u.timeFormat(orderForm.doortime, 'yyyy-mm-dd hh:MM:ss')}}</view>
						<view class="text" v-else>尽快上门</view>
					</view>
					<view class="item" v-if="orderForm.brotherdoortime">
						<text class="label">小哥上门：</text>
						<view class="text">{{$u.timeFormat(orderForm.brotherdoortime, 'yyyy-mm-dd hh:MM:ss')}}</view>
					</view>
					<view class="item">
						<text class="label">需求回收品：</text>
						<view class="text">
							<u-tag 
								class="u-m-l-5"
								v-for="(item, index) in orderForm.estimated" 
								:key="index"
								:text="item.names" 
								type="info" 
								size="mini"
							/>
						</view>
					</view>
					<view class="item" @click="seePhoto([orderForm.pics],0)">
						<text class="label">用户拍照：</text>
						<view class="text">
							<view class="u-flex" style="justify-content: flex-end;">
								<view class="u-m-l-10" v-for="(item,index) in orderForm.pics" :key="index">
									<u-image width="60rpx" height="60rpx" :src="utils.getPic(item)" shape="circle" @click="seePhoto(orderForm.pics,index)"></u-image>
								</view>
							</view>
						</view>
					</view>
					<view class="item" @click="seePhoto([orderForm.signinpic],0)">
						<text class="label">小哥上门拍照：</text>
						<view class="text">
							<view class="u-flex" style="justify-content: flex-end;">
								<view class="u-m-l-10">
									<u-image width="60rpx" height="60rpx" shape="circle" :src="utils.getPic(orderForm.signinpic)"></u-image>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>

			<view class="grid-box">
				<view class="u-table-box">
					<view class="head-title u-m-b-20"><text>回收汇总</text></view>
					<u-button size="medium" plain @click="openPopup">+添加可回收物</u-button>
					
					<goods-list :orderId="orderId" :listData="confirmForm.details" @upDate="upDate"></goods-list>
				</view>
				<view class="total">
					<text>共{{confirmForm.details.length}}种回收品 合计:</text>
					<text class="total-price">
						￥{{!confirmForm.total?'--':confirmForm.total.toFixed(2)}}
					</text>
				</view>
			</view>
			
			<view class="grid-box">
				<view class="head-title u-m-b-20"><text>订单备注</text></view>
				<view class="msg-box">
					<u-input v-model="confirmForm.remarks" type="textarea" border input-align="left" placeholder="请输入备注" />
				</view>
			</view>
			<view class="grid-box" v-if="process.length > 0">
				<view class="head-title u-m-b-30"><text>订单进程</text></view>
				<u-time-line>
					<u-time-line-item v-for="(item,index) in process" :key="index">
						<!-- 此处没有自定义左边的内容，会默认显示一个点 -->
						<template v-slot:content>
							<view>
								<view class="u-order-title u-m-b-10">{{item.state}}</view>
								<view class="u-order-desc" v-if="item.remarks">
									<text v-if="item.state == '已取消'">取消原因：</text>
									<text>{{item.remarks}}</text>
								</view>
								<view class="u-order-time"><u-icon name="calendar-fill"></u-icon>{{$u.timeFormat(item.createddatetime, 'yyyy-mm-dd hh:MM:ss')}}</view>
							</view>
						</template>
					</u-time-line-item>
				</u-time-line>
			</view>
		</view>
		<view class="navigation">
			<view class="right">
				<!-- <u-button class="navbtn">联系客服</u-button> -->
				<u-button v-if="confirmForm.details.length > 0" type="primary" class="navbtn" @click="startConfimCreate">
					<text>确认结算</text>
				</u-button>
				<u-button v-else type="primary" class="navbtn" @click="openPopup">
					<text>添加可回收物</text>
				</u-button>
			</view>
		</view>
		<!-- 分拣清算 -->
		<!-- <u-popup v-model="chooseShow" mode="bottom" border-radius="14">
			<goods-choose :orderId="orderId" :details="confirmForm.details" @confirm="confirmChoose"></goods-choose>
		</u-popup> -->
		<!-- 确认支付方式 -->
		<u-popup v-model="payTypeShow" mode="center" border-radius="14" :closeable="true" width="600rpx" z-index="100">
			<view class="u-p-t-30 u-p-b-30 u-text-center"><text class="u-font-32">支付方式</text></view>
			<u-divider></u-divider>
			<u-cell-group>
				<u-cell-item title="微信支付" :arrow="false" @click="choosePayType(1)">
					<view slot="icon" class="u-flex" style="width: 80rpx;">
						<u-icon name="weixin-fill" size="60" color="#28a745"></u-icon>
					</view>
					<view slot="label">
						<view v-if="onlinePay"><text>支付至用户账户余额</text></view>
						<view v-else><text class="u-type-error">余额不足</text></view>
					</view>
					<view slot="right-icon">
						<view class="circle" :class="payType == 1?'on':''">
							<u-icon name="checkmark" size="20" color="#fff" v-if="payType == 1"></u-icon>
						</view>
					</view>
				</u-cell-item>
				<u-cell-item title="现金支付" :arrow="false" @click="choosePayType(2)">
					<view slot="icon" class="u-flex" style="width: 80rpx;">
						<u-icon name="rmb-circle-fill" size="60" color="#fd7e14"></u-icon>
					</view>
					<view slot="label">
						<view><text>与用户线下现金结算</text></view>
					</view>
					<view slot="right-icon">
						<view class="circle" :class="payType == 2?'on':''">
							<u-icon name="checkmark" size="20" color="#fff" v-if="payType == 2"></u-icon>
						</view>
					</view>
				</u-cell-item>
			</u-cell-group>
			<view class="u-p-30">
				<u-button type="primary" @click="recovery(payType)" :loading="loading">确认支付</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import uniCopy from '@/common/xb-copy/uni-copy.js'
	
	import {
		mapState
	} from 'vuex';
	import orderMap from '../orderMap/orderMap.vue';
	
	export default {
		components:{
			orderMap
		},
		props:{
			orderId: 0,
			orderForm: {}
		},
		data() {
			return {
				loading: false,
				process: [],
				// 分拣清算
				chooseShow: false,
				confirmForm:{
					orderid:0,
					details: [
						// {
						// 	id: 0,
						// 	weight: 0,
						// 	price: 0
						// }
					],
					total: 0,
					remarks: ''
				},
				detailsArr:[],
				payTypeShow: false,
				payType: 1, //1:微信小程序,2:线下支付
				onlinePay: true, //是否可以使用微信支付
			}
		},
		computed: {
			...mapState({
				userdetail: state => state.userdetail,
				header: state => state.Authorization
			}),
			...mapState('brother', {
				goodsDetails: state => state.goodsDetails,
			})
		},
		watch: {
			goodsDetails(value){
				let confirmForm = {
					orderid: this.orderId,
					details: value
				}
				this.confirmChoose(confirmForm)
			}
		},
		created() {
			// this.confirmForm.details = this.goodsDetails
			this.getProcess()
			this.getQuota()
		},
		methods: {
			// 复制方法
			copy(font) {
				uniCopy({
					content:font,
					success:(res)=>{
						uni.showToast({
							title: res,
							icon: 'none'
						})
					},
					error:(e)=>{
						uni.showToast({
							title: e,
							icon: 'none',
							duration:3000,
						})
					}
				})
			},
			// 获取订单流程
			getProcess(){
				this.$u.get('/User/Order/Process?id=' + this.orderId).then((res)=>{
					this.process = res
				})
			},
			// 查看地图
			goMap(data){
				// uni.navigateTo({
				// 	url: `/pages/brother/order/views/map/map?lat=${data.lat}&lng=${data.lng}`
				// })
				
				uni.openLocation({
					latitude: data.lat,
					longitude: data.lng,
					name: data.address,
					// address: data.citynames,
					success: function () {
						console.log('success');
					}
				});
			},
			// 打电话
			call(phone){
				uni.makePhoneCall({
					phoneNumber:phone
				})
			},
			// 打开分拣清算列表
			openPopup(){
				// this.chooseShow = true
				// this.confirmForm.details = this.goodsDetails
				this.$store.commit('brother/setGoodsDetails', this.confirmForm.details)
				if(this.orderForm.enterprise){
					uni.navigateTo({
						url: `/pages/brother/goodsChoose/goodsChoose?orderId=${this.orderId}&enterpriseid=${this.orderForm.enterprise.id}&citypath=${this.orderForm.address.citypath}`,
					});
				}else{
					uni.navigateTo({
						url: `/pages/brother/goodsChoose/goodsChoose?orderId=${this.orderId}&userid=${this.orderForm.user.id}&citypath=${this.orderForm.address.citypath}`,
					});
				}
				
				// uni.navigateTo({
				// 	url: `/pages/brother/goodsChoose/goodsChoose?orderId=${this.orderId}`,
				// });
			},
			// 删除分拣清算列表
			// delGoods(index){
			// 	this.confirmForm.details.splice(index,1)
			// 	// 重新计算合计总价
			// 	this.confirmForm.total = 0
			// 	this.confirmForm.details.forEach((item)=>{
			// 		this.confirmForm.total += item.weight*item.price
			// 	})
			// },
			// 关闭分拣清算窗口
			confirmChoose(confirmForm){
				this.confirmForm.orderid = this.orderId
				let historyArr = []
				if(this.confirmForm.details.length > 0){
					historyArr = JSON.parse(JSON.stringify(this.confirmForm.details))
				}
				this.confirmForm.details = confirmForm.details
				
				this.confirmForm.total = 0
				confirmForm.details.forEach((item, index) => {
					// this.getPriceByOrder(item.id).then((datas) => {
					// 	this.$set(this.confirmForm.details[index], 'allowedit', datas.edit)
					// 	this.$set(this.confirmForm.details[index], 'highprice', datas.highprice)
					// 	this.$set(this.confirmForm.details[index], 'minprice', datas.minprice)
					// 	if(datas.price == 0){
					// 		this.$set(this.confirmForm.details[index], 'price', datas.minprice)
					// 	}else{
					// 		this.$set(this.confirmForm.details[index], 'price', datas.price)
					// 	}
					// 	historyArr.forEach((ha,i)=>{
					// 		if(ha.id == item.id){
					// 			item.weight = ha.weight
					// 			item.price = ha.price
					// 		}
					// 	})
					// })
					
					this.confirmForm.total += item.weight*item.price
					
				})
				this.chooseShow = false
			},
			upDate(confirmForm){
				this.confirmForm.orderid = this.orderId
				this.confirmForm.details = confirmForm.details
				
				this.confirmForm.total = 0
				this.confirmForm.details.forEach((item, index) => {
					this.confirmForm.total += item.weight*item.price
				})
				this.confirmForm.remarks = confirmForm.remarks
				this.$store.commit('brother/setGoodsDetails', this.confirmForm.details)
				this.chooseShow = false
			},
			//获取小类价格
			getPriceByOrder(productId) {
				uni.showLoading({
					title: '获取价格中...'
				})
				let api = ''
				if(this.orderForm.enterprise){
					api = `/Waste/Price/Get?productId=${productId}&enterpriseid=${this.orderForm.enterprise.id}&citypath=${this.orderForm.address.citypath}`
				}else{
					api = `/Waste/Price/Get?productId=${productId}&userid=${this.orderForm.user.id}&citypath=${this.orderForm.address.citypath}`
				}
				return this.$u.get(api).then((res) => {
						uni.hideLoading()
					return res
				})
			},
			// 发起提交
			startConfimCreate(){
				if(this.confirmForm.details.length == 0){
					uni.showToast({
						title: '请先分拣清算',
						icon: 'none'
					});
					this.chooseShow = true
					return
				}
				
				// 方式1：选择支付方式（1、微信小程序 2、现金支付）
				// this.getQuota().then((res)=>{
				// 	// 判断是否有微信支付配额
				// 	if (res.balance == null || res.balance > 0){
				// 		// 可使用微信支付
				// 		this.onlinePay = true
				// 		this.payType = 1
				// 	}else{
				// 		// 不可以使用微信支付
				// 		this.onlinePay = false
				// 		this.payType = 2
				// 	}
					
				// 	this.payTypeShow = true
				// })
				
				// 方式2：直接微信小程序支付
				// this.getQuota().then((res)=>{
				// 	// 判断是否有微信支付配额
				// 	if (res.balance == null || res.balance > 0){
				// 		// 可使用微信支付
				// 		this.onlinePay = true
				// 		// this.payType = 1
				// 	}else{
				// 		// 不可以使用微信支付
				// 		this.onlinePay = false
				// 		// this.payType = 2
				// 	}
					
				// 	this.recovery(1)
				// })
				
				this.recovery(1)
			},
			// 获取小哥今日微信支付配额余额
			getQuota(){
				return this.$u.get('/Brother/GetQuota').then((res)=>{
					/*
						quota				小哥配额（0为不限）
						amount				小哥已付金额（已用配额）
						balance				小哥剩余配额(空为不限，0为没有钱了，不能微信支付)
						franchiseBalance	加盟商剩余配额(空为不限，0为没有钱了，不能微信支付)
						operatorBalance		运营商账户余额（0为没有钱了，不能微信支付）
					*/
					console.log('查询剩余线上余额',res)
					return res
				})
			},
			// 选择支付方式
			choosePayType(type){
				if(type == 1){
					this.payType = type
				}else{
					if(this.onlinePay == false){
						uni.showToast({
							title: '余额不足，请选择现金支付',
							icon: 'none'
						})
						this.payType = 2
					}else{
						this.payType = type
					}
				}
			},
			// 确认回收
			recovery(payWay = 1){
				uni.showModal({
					title: `${payWay == 1?'微信支付结算':'现金结算'}`,
					content: '您确定要结算订单吗?',
					success: (res) => {
						if (res.confirm) {
							let details = []
							this.confirmForm.details.forEach((item)=>{
								details.push({
									id: item.id,
									typeRemarks: item.typeremarks,
									price: item.price,
									weight: item.weight
								})
							})
							this.$u.post('/User/Order/Recovery',{
								details: details,
								orderid: this.orderId,
								remarks: this.confirmForm.remarks,
								payWay: payWay
							}).then((res)=>{
								uni.showToast({
									title: res.msg
								})
								this.$store.commit('brother/setGoodsDetails', [])
								uni.redirectTo({
									url: '/pages/brother/order/success/success?id='+this.orderId
								})
								// this.$emit('update')
							})
						}
					}
				})
			},
			seePhoto(imgArr = [],current) {
				let url = []
				imgArr.forEach((item)=>{
					url.push(this.utils.getPic(item))
				})
				uni.previewImage({
					current: current,
					urls: url
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.order-views{
		position: relative;
		top: -100rpx;
		margin-bottom: -100rpx;
	}
	// 订单
	.order {
		// width: 690rpx;
		// background-color: #ffffff;
		// margin: 30rpx auto;
		// border-radius: 30rpx;
		// box-sizing: border-box;
		// padding: 30rpx;
		// font-size: 28rpx;
		// box-shadow: $u-color-border-shadow;
		.top {
			display: flex;
			justify-content: space-between;
			.left {
				display: flex;
				align-items: center;
				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}
			.right {
				color: $u-type-warning-dark;
			}
		}
		.item {
			display: flex;
			margin: 20rpx 0 0;
			justify-content: space-between;
			.left {
				margin-right: 20rpx;
				image {
					width: 160rpx;
					height: 160rpx;
					border-radius: 10rpx;
				}
			}
			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}
				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}
				.delivery-time {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;
				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}
				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}
		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;
			.total-price {
				font-size: 32rpx;
				color: $u-color-orange;
			}
		}
		.time{
			font-size: 24rpx;
			margin: 20rpx 0;
			line-height: 40rpx;
		}
		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;
			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
				padding: 0 10rpx;
			}
			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}
	// 按钮栏
	.navigation {
		display: flex;
		margin-top: 30rpx;
		border: solid 2rpx #f2f2f2;
		background-color: #ffffff;
		position: sticky;
		bottom: 0;
		z-index: 99;
		.right {
			display: flex;
			font-size: 28rpx;
			align-items: center;
			flex: 1;
			justify-content: space-around;
			padding: 20rpx;
			.navbtn{
				width: 100%;
				margin: 0 10rpx;
			}
			.btn {
				line-height: 66rpx;
				padding: 0 30rpx;
				border-radius: 36rpx;
				color: #ffffff;
				margin: 16rpx 0;
			}
			.cancel {
				margin-right: 30rpx;
				color: $u-tips-color;
			}
			.buy {
				background-color: #ff7900;
			}
		}
	}
	// 
	.u-table-box{
		margin: 20rpx 0;
		.green{
			background-color: $u-color-green;
			color: #fff!important;
		}
		.red{
			background-color: $u-color-red;
			color: #fff!important;
		}
	}
	.total {
		margin-top: 20rpx;
		text-align: right;
		font-size: 24rpx;
		.total-price {
			font-size: 32rpx;
			color: $u-color-orange;
		}
	}

	.u-line{
		border-bottom: 1px solid $u-border-color;
		padding-top: 30rpx;
		margin-bottom: 20rpx;
	}
	
	.u-order-title {
		color: #333333;
		font-weight: bold;
		font-size: 32rpx;
	}
	
	.u-order-desc {
		color: rgb(150, 150, 150);
		font-size: 28rpx;
		margin-bottom: 6rpx;
	}
	
	.u-order-time {
		color: rgb(200, 200, 200);
		font-size: 26rpx;
	}
	.circle{
		width: 40rpx;
		height: 40rpx;
		border: 1px solid #666;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		&.on{
			border-color: $u-color-blue;
			background-color: $u-color-blue;
		}
	}
</style>
